/**
 * 
 */
package de.riceman.simpleGo.players;

import de.riceman.simpleGo.main.BitMatrix;

/**
 * @author riceman
 *
 */
public class TemperatureMatrix {

	private double temperature[][];
	private double zeroMatrixTemperature;
	
	public TemperatureMatrix (int size) {
		temperature = new double[size][size];
		
		for (int i = 0; i < temperature.length; i++) {
			for (int j = 0; j < temperature.length; j++) {
				temperature[i][j] = 0.5;
			}
		}
		
		zeroMatrixTemperature = 0.5;
	}
	
	public double getTemperature(int i, int j) {
		
		return temperature[i][j];
	}
	
	public double getZeroMatrixTemperature() {
		return zeroMatrixTemperature;
	}

	public void setZeroMatrixTemperature(double zeroMatrixTemperature) {
		this.zeroMatrixTemperature = zeroMatrixTemperature;
	}

	public void apply(BitMatrix m, double value)  {
		
		if (m.getRows() == 1 && m.getColumns() == temperature.length * temperature.length) {
			
			if (m.isZeroMatrix()) {
				zeroMatrixTemperature = value;
			} else {
				for (int i = 0; i < temperature.length; i++) {
					for (int j = 0; j < temperature.length; j++) {
	
						if (m.get((i*temperature.length) + j)) {
							temperature[i][j] = value;
						}
					}
				}
			}			
		}
	}
	
	public void multiply(BitMatrix m, double value)  {
		
		if (m.getRows() == 1 && m.getColumns() == temperature.length * temperature.length) {
			
			if (m.isZeroMatrix()) {
				zeroMatrixTemperature *= value;
			} else {
				for (int i = 0; i < temperature.length; i++) {
					for (int j = 0; j < temperature.length; j++) {
	
						if (m.get((i*temperature.length) + j)) {
							temperature[i][j] *= value;
						}
					}
				}
			}			
		}
	}
	
	public BitMatrix getWinners() {
		BitMatrix m = new BitMatrix(1, temperature.length*temperature.length);
		
		double highest = 0.0;
		
		for (int i = 0; i < temperature.length; i++) {
			for (int j = 0; j < temperature.length; j++) {
				if (temperature[i][j] > highest) {
					highest = temperature[i][j];
				}
			}	
		}
		
		if (zeroMatrixTemperature > highest) {
			highest = zeroMatrixTemperature;
		}
		
		//If zeroMatrixTemperature is the highest, the loop wouldn't find the highest value, and the zeroMatrix
		//would be returned.
		if (highest > 0.0) {
			for (int i = 0; i < temperature.length; i++) {
				for (int j = 0; j < temperature.length; j++) {
					if (temperature[i][j] == highest) {
						m.set((i*temperature.length)+ j);
					}
				}	
			}
		}
		
		return m;
	}
	
	public BitMatrix getNonZeroValues() {
		BitMatrix m = new BitMatrix(1, temperature.length*temperature.length);
		
		
		for (int i = 0; i < temperature.length; i++) {
			for (int j = 0; j < temperature.length; j++) {
				if (temperature[i][j] > 0) {
					m.set((i*temperature.length)+ j);
				}
			}	
		}
				
		return m;
	}
	
	public boolean isEmpty() {
		for (int i = 0; i < temperature.length; i++) {
			for (int j = 0; j < temperature.length; j++) {
				if (temperature[i][j] > 0.0) {
					return false;
				}
			}	
		}
		return true;
	}
	
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		StringBuffer out = new StringBuffer();
		
		for (int i = 0; i < temperature.length; i++) {
			for (int j = 0; j < temperature.length; j++) {
				
				out.append(temperature[i][j] + " ");
				
			}
			out.append("\n");
		}
		
		return out.toString();
	}
}
